<?php

/**
 * SelectTest_Sql: this is quite a dummy test, because it only checks the resulting SQL string,
 * NOT the result of real SQL queries
 *
 * @author vbolshov
 */

use km\db\Select;

require_once dirname(__FILE__) . '/../../lib/km/db/Select.php';

class SelectTestSql extends PHPUnit_Framework_TestCase {
	function testSimplestSelect()
	{
		$s = new Select('test');
		$this->assertEquals('SELECT * FROM test', $s->sql());
	}
	function testConditionalSelect()
	{
		$s = new Select('test');
		$s->where('x = y');
		$this->assertEquals('SELECT * FROM test WHERE (x = y)', $s->sql());
	}
	function testConditionalSelectWithBoundVars()
	{
		$s = new Select('test');
		$s->where('x = ?', 'y')
			->where('x = ?', 1)
			->where('x = ?', false)
			->where('x = ?', null)
			;
		$this->assertEquals('SELECT * FROM test WHERE (x = ?) AND (x = ?) AND (x = ?) AND (x = ?)', $s->sql());
		$this->assertEquals('SELECT * FROM test WHERE (x = \'y\') AND (x = 1) AND (x = 0) AND (x = NULL)', $s->__toString());
	}
	function testBindingArrayValues()
	{
		$s = new Select('test');
		$s->where('x IN (?)', array(1, 2, 3))
			;
		$this->assertEquals('SELECT * FROM test WHERE (x IN (?,?,?))', $s->sql());
		$this->assertEquals('SELECT * FROM test WHERE (x IN (1,2,3))', $s->__toString());
	}
	function testSimpleJoin()
	{
		$s = new Select('test');
		$s->join('JOIN x ON (x.id = test.id)');
		$this->assertEquals('SELECT * FROM test JOIN x ON (x.id = test.id)', $s->sql());
	}
	function testJoinWithBoundVars()
	{
		$s = new Select('test');
		$s->join('JOIN x ON (x.id = ? OR x.id IN (?))', null, array(1, array(1, 2, 3)));
		$this->assertEquals('SELECT * FROM test JOIN x ON (x.id = 1 OR x.id IN (1,2,3))', $s->__toString());
	}
	function testGroupBy()
	{
		$s = new Select('test');
		$s->group('id')
			->group(array('name'));
		$this->assertEquals('SELECT * FROM test GROUP BY id, name', $s->__toString());
	}
	function testSimpleHaving()
	{
		$s = new Select('test');
		$s->having('id > 0');
		$this->assertEquals('SELECT * FROM test HAVING (id > 0)', $s->__toString());
	}
	function testHavingWithBoundVars()
	{
		$s = new Select('test');
		$s->having('id > ?', 1);
		$this->assertEquals('SELECT * FROM test HAVING (id > ?)', $s->sql());
		$this->assertEquals('SELECT * FROM test HAVING (id > 1)', $s->__toString());
	}
	function testOrder()
	{
		$s = new Select('test');
		$s->order('id')
			->order(array('name DESC'));
		$this->assertEquals('SELECT * FROM test ORDER BY id, name DESC', $s->__toString());
	}
	function testLimit()
	{
		$s = new Select('test');
		$s->limit(10, 20);
		$this->assertEquals('SELECT * FROM test LIMIT 10 OFFSET 20', $s->__toString());
	}
	function testPagination()
	{
		$s = new Select('test');
		$s->paginate(2, 10);
		$this->assertEquals('SELECT * FROM test LIMIT 10 OFFSET 10', $s->__toString());
	}
}